class IUP_SPLIT
-- Creates a void container that split its client area in two. Allows the 
-- provided controls to be enclosed in a box that allows expanding and 
-- contracting the element size in one direction, but when one is expanded the 
-- other is contracted.
--
-- It does not have a native representation, but it contains also a IUP_CANVAS 
-- to implement the bar handler.

inherit
	IUP_CONTAINER
		redefine
			execute_valuechanged
		end
	IUP_WIDGET_EXPAND
	IUP_WIDGET_WID
	IUP_WIDGET_FONT
	IUP_WIDGET_SIZE
	IUP_WIDGET_RASTERSIZE
	IUP_WIDGET_USERSIZE
	IUP_WIDGET_CLIENTSIZE
	IUP_WIDGET_CLIENTOFFSET
	IUP_WIDGET_POSITION
	IUP_WIDGET_MAXMIN_SIZE
	IUP_WIDGET_CHILD
	IUP_WIDGET_NAME
	IUP_WIDGET_CUSTOM_ATTRIBUTES

create {ANY}
	split_empty,
	split

feature {ANY}

	split_empty
	   -- Create an empty sbox.
		local
			p, a_split: POINTER
		do
			a_split := int_split (p, p)
			set_widget(a_split)
		end
	
	split (child1, child2: IUP_WIDGET)
	   -- Create a new split containing the widgets.
		local
         a_split: POINTER
		do
			a_split := int_split (child1.widget, child2.widget)
			set_widget(a_split)
		end

	-- Attributes

	set_auto_hide (state: BOOLEAN)
		-- (non inheritable): if the child client area is smaller than the bar 
		-- size, then automatically hide the child. Default: False.
		do
			iup_open.set_attribute(Current, "AUTOHIDE", boolean_to_yesno(state))
		end

	is_auto_hide: BOOLEAN
		-- Return the state of AUTOHIDE.
		local
			str: STRING
		do
			str := iup_open.get_attribute(Current, "AUTOHIDE")
			Result := yesno_to_boolean(str)
		end

	set_bar_size (size: INTEGER)
		-- (non inheritable): controls the size of the bar handler. Default: 5.
		require
			size >= 0
		do
			iup_open.set_attribute(Current, "BARSIZE", size.to_string)
		end

	get_bar_size: INTEGER
		-- Return the size of the bar.
		local
			str: STRING
		do
			str := iup_open.get_attribute(Current, "BARSIZE")
			Result := str.to_integer
		end

	set_rgb_color (red, green, blue: INTEGER)
		-- Changes the color of the bar grip affordance. The value should be given
		-- in "R G B" color style. Default: "192 192 192".
		do
			iup_open.set_attribute(Current, "COLOR", rgb_to_string(red, green, blue))
		end
	
	get_rgb_color: TUPLE[INTEGER, INTEGER, INTEGER]
		-- Return the RGB values of the bar grip color.
		do
			Result := iup_open.get_rgb(Current, "COLOR")
		end

	set_horizontal_orientation
		-- (creation only): Indicates an horizontal orientation of the bar 
		-- handler. The direction of the resize is perpendicular to the 
		-- orientation.
		do
			iup_open.set_attribute(Current, "ORIENTATION", "HORIZONTAL")
		end

	set_vertical_orientation
		-- (creation only): Indicates the orientation of the bar handler. The 
		-- direction of the resize is perpendicular to the orientation. This is 
		-- the default value.
		do
			iup_open.set_attribute(Current, "ORIENTATION", "VERTICAL")
		end

	is_horizontal: BOOLEAN
		local
			str: STRING
		do
			str := iup_open.get_attribute(Current, "ORIENTATION")

			if str.is_equal("HORIZONTAL") then
				Result := True
			else
				Result := False
			end
		end

	is_vertical: BOOLEAN
		local
			str: STRING
		do
			str := iup_open.get_attribute(Current, "ORIENTATION")

			if str.is_equal("VERTICAL") then
				Result := True
			else
				Result := False
			end
		end

	set_layout_drag (state: BOOLEAN)
		-- (non inheritable): When the bar is moved automatically update the 
		-- children layout. Default: True. If set to False then the layout will be
		-- updated only when the mouse drag is released.
		do
			iup_open.set_attribute(Current, "LAYOUTDRAG", boolean_to_yesno(state))
		end

	is_layout_drag: BOOLEAN
		-- Return the state of LAYOUTDRAG.
		local
			str: STRING
		do
			str := iup_open.get_attribute(Current, "LAYOUTDRAG")
			Result := yesno_to_boolean(str)
		end

	set_min_and_max (min: INTEGER; max: INTEGER)
		-- (non inheritable): sets minimum and maximum crop values for VALUE. The 
		-- min value can not be less than 0, and the max value can not be larger 
		-- than 1000. This will constrain the interactive change of the bar 
		-- handler. Default: "0:1000".
		require
			non_negative: min >= 0
							  max >= 0
			less:			  min <= 1000
							  max <= 1000
							  min <= max
		local
			size: STRING
		do
			size := min.to_string
			size.append_string(":")
			size.append_string(max.to_string)

			iup_open.set_attribute(Current, "MINMAX", size)
		end

	get_min_and_max: TUPLE[INTEGER, INTEGER]
		-- Return the minmax size.
		local
			size: STRING
		do
			size := iup_open.get_attribute(Current, "MINMAX")
			Result := components_of_size(size)
		end

	set_show_grip (value: STRING)
		-- (non inheritable): Shows the bar grip affordance. Default: YES. When 
		-- set to NO, the BARSIZE is set to 3. When set to NO if COLOR is defined 
		-- then it is used to fill the grip area (since 3.11.1). If set to 
		-- "LINES" then instead of the traditional grip appearance, it will be 
		-- two parallel lines
		require
			is_valid_showgrip(value)
		do
			iup_open.set_attribute(Current, "SHOWGRIP", value)
		end

	get_show_grip: STRING
		-- Return the value of SHOWGRIP.
		do
			Result := iup_open.get_attribute(Current, "SHOWGRIP")
		end

	set_value (value: INTEGER)
		-- (non inheritable): The proportion of the left or top (child1) client 
		-- area relative to the full available area. It is an integer between 0 
		-- and 1000. If not defined, the Native size of the two children
		--	will define its initial size.
		require
			value >= 0
			value <= 1000
		do
			iup_open.set_attribute(Current, "VALUE", value.to_string)
		end

	get_value: STRING
		-- Return the value of VALUE.
		do
			Result := iup_open.get_attribute(Current, "VALUE")
		end

	-- Callback
	set_cb_value_changed (act: FUNCTION[TUPLE[IUP_SPLIT], STRING])
		local
			operation: INTEGER
		do
			cb_valuechanged := act

			if cb_valuechanged /= Void then
				operation := 1
			else
				operation := 0
			end
			
			iup_open.set_callback (Current, "VALUECHANGED_CB", "NONEEDED", operation)
		end

feature {IUP} -- Internal handle of callbacks

	execute_valuechanged: STRING
		do
			Result := cb_valuechanged.item([Current])
		end

feature {}

	-- For callback

	cb_valuechanged: FUNCTION[TUPLE[IUP_SPLIT], STRING]

	-- Internal
	
	int_split (child1, child2: POINTER): POINTER
		external "plug_in"
      alias "{
         location: "${sys}/plugins"
         module_name: "iup"
         feature_name: "IupSplit"
         }"
      end

	-- Validations

	is_valid_showgrip (value: STRING): BOOLEAN
		do
			if is_yes_no(value) or
				value.is_equal("LINES") then
				Result := True
			else
				Result := False
			end
		end

end

-- The MIT License (MIT)

-- Copyright (c) 2016, 2017 by German A. Arias

-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in 
-- all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.
